﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class CreateRequest : System.Web.UI.Page
{
    SqlConnection conn = null;
    protected void Page_Load(object sender, EventArgs e)
    {

            if (Session["Employee"] == null)
            {
                Response.Redirect("~/Login.aspx");
            }
            else
            {
                DateTime now = DateTime.Now;
                conn = Database.GetConnection();
                txtDate.Text = now.ToShortDateString();
                GetListManager();
        }
        

    }
    public void GetListManager()
    {
        SqlDataAdapter adapter = new SqlDataAdapter("select * from Employees where Roles_ID = 1", conn);
        DataTable tb = new DataTable();
        adapter.Fill(tb);
        if(!Page.IsPostBack)
        {
            ddllistManager.DataSource = tb;
            ddllistManager.DataTextField = "Emp_Username";
            ddllistManager.DataValueField = "Emp_ID";
            ddllistManager.DataBind();
        }

    }
    protected void btnCreate_Click(object sender, EventArgs e)
    {
        if (conn.State == System.Data.ConnectionState.Closed)
        {
            conn.Open();
        }
        DataTable dtEmployee = (DataTable)Session["Employee"];
        int idemployee = Convert.ToInt32(dtEmployee.Rows[0][0].ToString());
        int idmanager = Convert.ToInt32(ddllistManager.SelectedValue.ToString());
        SqlCommand comm = new SqlCommand("GetIdRequest", conn);
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.Add("@To",SqlDbType.Int).Value=idmanager;
        comm.Parameters.Add("@From", SqlDbType.Int).Value = idemployee;
        comm.Parameters.Add("@R_Price", SqlDbType.Int).Value = txtPrice.Text;
        string idrequest = comm.ExecuteScalar().ToString();
        Response.Redirect("Request.aspx?idrequest="+idrequest+"");
        conn.Close();
    }
}